/**
 * Represents the metadata associated to a Dart Frog application.
 */
export class DartFrogApplication {
  constructor(
    projectPath: string,
    port: number,
    vmServicePort: number,
    host?: string
  ) {
    this.projectPath = projectPath;
    this.host = host === undefined ? "localhost" : host;
    this.port = port;
    this.vmServicePort = vmServicePort;
  }

  /**
   * The root path to the application's project directory.
   */
  public readonly projectPath: string;

  /**
   * The port on which the application is listening.
   */
  public readonly port: number;

  /**
   * The port on which the VM service is listening.
   */
  public readonly vmServicePort: number;

  /**
   * The host on which the application is bound to.
   */
  public readonly host: string;

  private _id: string | undefined;

  /**
   * The application's unique identifier.
   *
   * The application's unique identifier is generated by the Dart Frog daemon
   * upon starting the application.
   *
   * It is undefined until the application has been started by the Dart Frog
   * daemon, and in turn, has been assigned such identifier.
   */
  public get id(): string | undefined {
    return this._id;
  }

  /**
   * Sets the application's unique identifier.
   *
   * This method should only be called once, when the application is started and
   * has been assigned an identifier by the Dart Frog daemon.
   *
   * If the application already has an identifier, this method will do nothing.
   */
  public set id(value: string) {
    if (this._id) {
      return;
    }

    this._id = value;
  }

  private _vmServiceUri: string | undefined;

  /**
   * The URI of the VM service.
   *
   * This is used to attach to the debug session.
   *
   * It is undefined until the application has been started by the Dart Frog
   * daemon, and in turn, has been assigned such URI.
   */
  public get vmServiceUri(): string | undefined {
    return this._vmServiceUri;
  }

  /**
   * Sets the URI of the VM service.
   *
   * This method should only be called once, when the application is started and
   * has been assigned a VM service URI by the Dart Frog daemon.
   *
   * If the application already has a VM service URI, this method will do
   * nothing.
   */
  public set vmServiceUri(value: string) {
    if (this._vmServiceUri) {
      return;
    }

    this._vmServiceUri = value;
  }

  private _address: string | undefined;

  /**
   * The HTTP address of the Dart Frog application.
   *
   * This is used to launch the application in the browser.
   *
   * It is undefined until the application has been started by the Dart Frog
   * daemon, and in turn, has been assigned such address.
   */
  public get address(): string | undefined {
    return this._address;
  }

  /**
   * Sets the HTTP address of the Dart Frog application.
   *
   * This method should only be called once, when the application is started and
   * has been assigned an address by the Dart Frog daemon.
   *
   * If the application already has an address, this method will do nothing.
   */
  public set address(value: string) {
    if (this._address) {
      return;
    }

    this._address = value;
  }
}
